﻿#ifndef MEASUREDATASET_H
#define MEASUREDATASET_H
#include <QObject>
#include <QPointF>
#include <QPaintEvent>
#include <QSettings>
#include "MeasureView_global.h"
#include "MeasureElement.h"

namespace Qly {

class DialogMeasure;
class MeasureView;
class MEASUREVIEW_EXPORT MeasureDataSet : public QObject
{
    Q_OBJECT
    friend MeasureView;

public slots:
    void showMeasureDialog();
public:
    MeasureDataSet(MeasureView * view);

    int lineWidth() {return m_pen.width();}
    int textSize() {return m_font.pointSize();}
    QColor color() {return m_pen.color();}
    void loadSettings(QSettings &settings);
    void writeSettings(QSettings&settings);

    void setPixelSize(double newPixelSize_um, int method = 1);
    int setElementType(enum MeasureElement::ElementType type);
    void reset_id();
    int& element_id();
    void clearAll();
    void undo();
    void updateText();
    bool deleteElement(MeasureElement * element);//将一个测量对象删除掉,TODO 这个代码需要审查
    MeasureElement * deleteElement(QPointF p); //TODO 这个代码需要审查
    void setColor(QColor cl);
    void setLineWidth(int width);
    void setFont(QFont font);
    void setFontSize(int points);
    QList<MeasureElement *> elementList(); // 获得全部测量图元信息
signals:
    void elementAdded(MeasureElement *element); //当一个新的图元测量完成后发出这个信号
    void elementUpdated(MeasureElement *element); //当点到直线距离测量更新后发出这个信号，只有点到线测量会发出这个信号
    void elementDeleted(MeasureElement *element); // 一个测量图元被删除后发出这个信号, 这时 element 已经被删了。不要再访问这个指针指向的对象
protected:
    MeasureElement *findElement(const QPointF &pt); // 获得 pt 所在区域的元素指针
    void paint(QPainter &painter, int image_width, int image_height);
    int mouseLButtonPress(QPointF point_pix, QPointF point_um);
    int mouseRButtonPress(QPointF point_pix, QPointF point_um);
    int mouseMove(QPointF point_pix, QPointF point_um);
    int mouseRelease(QPointF point_pix, QPointF point_um);

    enum MeasureElement::ElementType m_elementType;
    QList<MeasureElement *> m_elementList;
    MeasureElement *m_pCurrentElement = nullptr;
    MeasureElement *m_pElementFind = nullptr;

    QPointF m_startPoint_pix;
    QPointF m_startPoint_um;

    QPen m_pen;
    QFont m_font;
    MeasureView * m_pView = nullptr;
    int m_element_id = 0; // id 计数，每次产生一个新的 element 时这个值加一

    DialogMeasure * m_pDialogMeasure = nullptr;

public:
    double m_pixSize_um = 1.0; // 图形元素根据这个来计算物理尺寸
    QString m_textTemplate_MeasureLine;
    QString m_textTemplate_MeasureParalleLine;
    QString m_textTemplate_MeasurePolygon;
    QString m_textTemplate_MeasurePolyline;

    QString m_textTemplate_MeasureCircleCenter;
    QString m_textTemplate_MeasureCircle2P;
    QString m_textTemplate_MeasureCircle3P;

    QString m_textTemplate_MeasureAngle3P;
    QString m_textTemplate_MeasureAngle4P;
    QString m_textTemplate_MeasureArc3P;

    QString m_textTemplate_MeasureCircle2Circle;

    QString m_textTemplate_MeasureDot2Line;


    QString m_textTemplate_MeasureRect;
    QString m_textTemplate_MeasureCount;

    UnitConvert m_unitConvert_MeasureLine;
    UnitConvert m_unitConvert_MeasureParallelLine;
    UnitConvert m_unitConvert_MeasurePolyLine;
    UnitConvert m_unitConvert_MeasurePolygon;

    UnitConvert m_unitConvert_MeasureCircleCenter;
    UnitConvert m_unitConvert_MeasureCircle3P;
    UnitConvert m_unitConvert_MeasureCircle2P;

    UnitConvert m_unitConvert_MeasureArc3P;
    UnitConvert m_unitConvert_MeasureAngle3P;
    UnitConvert m_unitConvert_MeasureAngle4P;

    UnitConvert m_unitConvert_MeasureCircle2Circle;
    UnitConvert m_unitConvert_MeasureDot2Line;
    UnitConvert m_unitConvert_MeasureRect;

    UnitConvert m_unitConvert_MeasureLineWidthSA;

    int m_lineWidthSA_width = 15;
};

inline int &MeasureDataSet::element_id()
{
    return m_element_id;
}

inline void MeasureDataSet::reset_id()
{
    m_element_id = 0;
}

inline void MeasureDataSet::setColor(QColor cl)
{
    m_pen.setColor(cl);
}

inline void MeasureDataSet::setLineWidth(int width)
{
    m_pen.setWidth(width);
}

inline void MeasureDataSet::setFont(QFont font)
{
    m_font = font;
}

inline void MeasureDataSet::setFontSize(int points)
{
    m_font.setPixelSize(points);
}

}


#endif // MEASUREDATASET_H
